
//
// 120314 - AH - (Inside) Created.
// 120317a- AH - (Huset *) Implements Serializable.
// 120317b- AH - (Huset) name(String).
// 120727 - AH - (Midtun *) EMPTY_ARRAY.
//

package org.soekrat.server.io.db;

import javax.persistence.Id;

/**
 * <p>
 *   Represents a Soekrat account. 
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class Account implements DbObject 
{
  private @Id Long id;
  
  private String role="visitor";
  
  private String name;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public Account(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param name Name on account holder.
   */
  public Account(String name)
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    
    this.name=name;
  }
  
  /**
   * <p>
   *   Gets unique of key of the account.
   * </p>
   * @return Key value or {@code -1} if not assigned (not stored in database).
   */
  public long id(){ return id==null ? -1 : id; }
  
  /**
   * <p>
   *   Role account has.
   * </p>
   * @return Role.
   */
  public String role(){ return role; }
  
  /**
   * <p>
   *   Gets name of account holder.
   * </p>
   * @return Name.
   */
  public String name(){ return name; }
  
  /**
   * <p>
   *   Sets name of account holder.
   * </p>
   * @param name Name.
   * @return {@code this}.
   */
  public Account name(String name)
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    
    name=name.trim();
    if (name.length()==0)
    {
      throw new NullPointerException("name can not be the empty string");
    }
    
    this.name=name;
    
    return this;
  }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();
    
    sb.append("id=").append(id()).append(", name=").append(name())
      .append(", role=").append(role());
    
    return sb.toString();
  }
  
  /**
   * <p>
   *   The empty array shared.
   * </p>
   */
  public static final Account[] EMPTY_ARRAY=new Account[0];
  
}
